//
//  AmuseViewController.swift
//  swiftDemo
//
//  Created by Gerry on 2022/1/9.
//

import UIKit
import SwiftyJSON
import SkeletonView
import SnapKit

class AmuseViewController: UIViewController {
    
    private lazy  var viewModel : AmuseViewModel = AmuseViewModel()

    private lazy var collectionView : UICollectionView = { [unowned self] in
        let layout = UICollectionViewFlowLayout()
        
        let collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.backgroundColor = UIColor.init(r: 240, g: 240, b: 240)
        collectionView.register(AmuseCell.self, forCellWithReuseIdentifier: "AmuseCell")
        collectionView.register(RecommendHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "RecommendHeaderView")
        
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.white
        setupUI()
        
        viewModel.requestData { isSuccess, msg in
            self.collectionView.reloadData()
        }
        
        view.isSkeletonable = true
        view.showSkeleton()
    }

}

extension AmuseViewController {
    func setupUI() {
        view.addSubview(self.collectionView);
        
        self.collectionView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
    }
}


extension AmuseViewController : UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      
        let m = viewModel.amuseList[section]
        return m.room_list!.count
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return viewModel.amuseList.count;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"AmuseCell", for: indexPath) as! AmuseCell;
//        cell.backgroundColor = randomColor()
        
        let model : HotCateModel = viewModel.amuseList[indexPath.section]
        cell.itemModel = model.room_list?[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let w = (kScreenW-30)/2.0
        let h = 150.0
        return CGSize.init(width: w, height: h)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView : RecommendHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "RecommendHeaderView", for: indexPath) as! RecommendHeaderView
        let model : HotCateModel = viewModel.amuseList[indexPath.section]
        headerView.titleLabel.text = model.tag_name ?? ""
        return headerView
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.init(width: kScreenW, height: 40)
    }
}

extension AmuseViewController : SkeletonCollectionViewDataSource {
    
    func numSections(in collectionSkeletonView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionSkeletonView(_ skeletonView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionSkeletonView(_ skeletonView: UICollectionView, cellIdentifierForItemAt indexPath: IndexPath) -> ReusableCellIdentifier {
        return "AmuseCell"
    }
    
    func collectionSkeletonView(_ skeletonView: UICollectionView, supplementaryViewIdentifierOfKind: String, at indexPath: IndexPath) -> ReusableCellIdentifier?  {
        return nil
    }
    
    func collectionSkeletonView(_ skeletonView: UICollectionView, skeletonCellForItemAt indexPath: IndexPath) -> UICollectionViewCell?  {
        let cell = skeletonView.dequeueReusableCell(withReuseIdentifier: "AmuseCell", for: indexPath) as? AmuseCell
        
        
        return cell
    }
    
    func collectionSkeletonView(_ skeletonView: UICollectionView, prepareCellForSkeleton cell: UICollectionViewCell, at indexPath: IndexPath) {
        let cell = cell as? AmuseCell
//        cell?.textField.isHidden = indexPath.row == 0
    }
}
